package com.woniuxy.bookmall.controller;

import com.woniuxy.bookmall.entity.TUser;
import com.woniuxy.bookmall.service.EmailService;
import com.woniuxy.bookmall.service.UserService;
import com.woniuxy.bookmall.utils.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Controller
@Slf4j
public class UserController {

    @Autowired
    UserService userService ;

    @Autowired
    EmailService emailService ;

    @Value("${spring.mail.username}")
    private String serverEmail ;

    @Autowired
    RedisUtils redisUtils ;

    @Autowired
    RabbitTemplate rabbitTemplate ;
    public static final String SEND_EMAIL_EXHCANGE = "send_email_exchange" ;
    public static final String SEND_EMAIL_QUEUE =  "send_email_queue";
    public static final String SEND_EMAIL_ROUTING_KEY = "send_email_routing_key" ;

    @RequestMapping("/user/login")
    @ResponseBody
    public String login(String username , String password, HttpServletRequest request){

        TUser user = userService.login(username,password);
        if (user == null) {
            request.setAttribute("errorInfo","用户名或者密码错误!");
            return "0" ;
        }else {
            //验证成功
            request.getSession().setAttribute("userInfo",user);
            //return "redirect:/book/index" ;// 重定向到 首页
            return "1" ;
        }

    }

   /* @RequestMapping("/user/login")
    public String login(String username , String password, HttpServletRequest request){

        TUser user = userService.login(username,password);
        if (user == null) {
            request.setAttribute("errorInfo","用户名或者密码错误!");
            return "login" ;
        }else {
            //验证成功
            request.getSession().setAttribute("userInfo",user);
            //return "redirect:/book/index" ;// 重定向到 首页
            return "redirect:/book/index" ;
        }
    }*/

    @RequestMapping("/user/logout")
    public String logout(HttpServletRequest request){
        //可以做 业务的处理
        request.getSession().invalidate();//清空session中的数据
        return "redirect:/book/index" ;
    }

    @RequestMapping("/user/regView") // http://localhost:9999/user/
    public String regView(int flag ,HttpServletRequest request) {
        request.setAttribute("flag",flag);
        return "login" ;  // login.html  http://localhost:9999/
    }

    @RequestMapping("/user/sendEmailCode")
    @ResponseBody
    public String sendEmailCode(String email){
        TUser user = userService.findUserByEmail(email);
        if (user != null) {
            return "0" ;
        }else {
            //发送验证码  并且存储到redis
            /*String code = emailService.sendEmail(serverEmail,email);
            log.info("您生产的验证码是:{}",code);*/

            try {
                //发送mq消息 到队列
                Map<String,Object> msg = new HashMap<>();
                msg.put("fromEmail",serverEmail);
                msg.put("toEmail",email);
                rabbitTemplate.convertAndSend(SEND_EMAIL_EXHCANGE, SEND_EMAIL_ROUTING_KEY, msg);
            }catch (Exception e) {
                e.printStackTrace();
                return "-1" ;
            }
            return "1" ;
        }
    }

    @RequestMapping("/user/validationEmail")
    @ResponseBody
    public boolean validationEmail(String email){
        TUser user = userService.findUserByEmail(email);
        if (user == null) {
            return  true ;
        }
        return false ;
    }

    @RequestMapping("/user/reg")
    @ResponseBody
    public String register(String email,String password,String emailCode){
        try {
            String code = (String) redisUtils.get(email);
            if (code != null && emailCode.equals(code)) {
                userService.register(email,password);
                return "1" ;
            }else {
                return "0" ;
            }
        }catch (Exception e){
            e.printStackTrace();
            return "2" ;
        }
    }



}